﻿@layout ManagerLayout

@using EmployeeLeaveSystem.Shared;
@using EmployeeLeaveSystem.Shared.Models;
@inject IMessageService _MessageService;
@inject HttpClient _HttpClient;
@inject ModalService _modalService;

<h5>部门管理面板</h5>
<Table DataSource="listDepartments" TableLayout="fixed" RowClassName="@(_=>"editable-row")" Bordered ScrollX="800px">
    <ChildContent Context="context">
        <Column TData="string" Title="Id">
            @context.Id
        </Column>
        <Column TData="string" Title="Name">
            @if (!editCache[context.Id].edit) {
                @context.Name
            } else {
                <Input @bind-Value="editCache[context.Id].data.Name" />
            }
        </Column>
        <Column TData="string" Title="描述">
            @if (!editCache[context.Id].edit) {
                @context.Description
            } else {
                <Input @bind-Value="editCache[context.Id].data.Description" />
            }
        </Column>
      
        <ActionColumn Title="Action">
            @if (!editCache[context.Id].edit) {
                <a @onclick="() => startEdit(context.Id)">Edit</a>
                <Popconfirm Title="Sure to Delete?"
                        OnConfirm="() => Delete(context.Id)"
                        OkText="Yes"
                        CancelText="No">
                    <a>Delete</a>
                </Popconfirm>
            } else {
                <a @onclick="() => saveEdit(context.Id)" class="save">Save</a>
                <Popconfirm Title="Sure to cancel?"
                        OnConfirm="() => cancelEdit(context.Id)"
                        OkText="Yes"
                        CancelText="No">
                    <a>Cancel</a>
                </Popconfirm>
            }
        </ActionColumn>
    </ChildContent>
</Table>
<h5>新增一个部门</h5>
<Form Model="department" OnFinish="AddDepartment">
    <FormItem Label="部门">
        <Input @bind-Value=department.Name />
    </FormItem>
    <FormItem Label="描述">
        <Input @bind-Value=department.Description />
    </FormItem>
    <Button HtmlType="submit">新增</Button>
</Form>
@code {
    List<Department> listDepartments = new();
    Department department = new();
    IDictionary<int, (bool edit, Department data)> editCache = new Dictionary<int, (bool edit, Department data)>();

    protected override async Task OnInitializedAsync() {
        listDepartments = await _HttpClient.GetFromJsonAsync<List<Department>>("api/Auth/GetAllDepartments");
        foreach (var item in listDepartments) {
            editCache[item.Id] = (false, item);
        }
        _MessageService.Success("加载完毕");
        await base.OnInitializedAsync();
    }
    // 表格编辑
    void startEdit(int id) {
        var data = editCache[id];
        editCache[id] = (true, data.data); // add a copy in cache
    }

    void cancelEdit(int id) {
        var data = listDepartments.FirstOrDefault(item => item.Id == id);
        editCache[id] = (false, data); // recovery
    }

    async void saveEdit(int id) {
        var index = listDepartments.FindIndex(item => item.Id == id);
        listDepartments[index] = editCache[id].data; // apply the copy to data source
        editCache[id] = (false, listDepartments[index]); // don't affect rows in editing

        // 上传到服务器
        await _HttpClient.PostAsJsonAsync("api/Auth/UpdateDepartment", listDepartments[index]);
        await _MessageService.Success("更新成功");
    }
    async void Delete(int id) {
        try {
            var res = await _HttpClient.GetAsync($"api/Auth/DeleteDepartment?id={id}");
            if (res.StatusCode == System.Net.HttpStatusCode.OK) {
                listDepartments.Remove(listDepartments[id]);
                editCache.Remove(id);
                await _MessageService.Success("删除人员成功");
            } else {
                await _MessageService.Error("失败！因为现在删除的对象，拥有一些关联系，再未能清除之前，无法删除");
            }
        }
        catch (Exception ex) {
            await _MessageService.Error("失败！因为现在删除的人，拥有一些关联，再未能清除之前，无法删除");
        }
    }
    void AddDepartment() {
        _HttpClient.PostAsJsonAsync("api/Auth/AddDepartment", department);
    }
}